package com.ncr.travel.appmgr.service.converter.impl;

import java.util.Date;
import java.util.Set;

import org.junit.Before;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.ncr.travel.appmgr.service.config.AppManagerConfigurationNodeBuilder;
import com.ncr.travel.tas.configuration.model.ConfigurationNode;
import com.ncr.travel.tas.configuration.model.ConfigurationValue;
import com.ncr.travel.tas.shared.configuration.ConfigurationNodeEnum;
import com.ncr.travel.tas.shared.configuration.ConfigurationProductKey;

public abstract class AbstractConverterTestCase {
	protected static final Logger logger = LoggerFactory.getLogger(AbstractConverterTestCase.class);

	protected ConfigurationNode applicationManagerNode;

	@BeforeClass
	public static void setupClass() {
	}

	@Before
	public void setup() {
		applicationManagerNode = new ConfigurationNode(ConfigurationNodeEnum.APPMGR);
		String author = AppManagerConfigurationNodeBuilder.class.getCanonicalName();
		applicationManagerNode.setName(ConfigurationProductKey.APPLICATION_MANAGER_UI);
		applicationManagerNode.setDescription("Application Manager UI Settings");
		applicationManagerNode.setUpdatedBy(author);
		applicationManagerNode.setUpdatedOn(new Date());

		// create the products right underneath
		ConfigurationValue productKeys = AppManagerConfigurationNodeBuilder.createProducts();
		applicationManagerNode.addConfiguration(productKeys);
	}

	protected ConfigurationValue getConfigurationValue(ConfigurationProductKey key, String path) {
		Set<ConfigurationValue> productConfigurations = applicationManagerNode.getConfigurations();

		// get all products
		ConfigurationValue products = productConfigurations.toArray(new ConfigurationValue[productConfigurations.size()])[0];
		// find product by its key
		ConfigurationValue productConfig = products.findChild(key);
		ConfigurationValue configValue = productConfig;
		// get path tokens for traversing config values
		String[] tokens = StringUtils.tokenizeToStringArray(path, "&");

		// search for config value based on path tokens
		for (String token : tokens) {
			configValue = configValue.findChild(token);
		}
		return configValue;
	}
}
